import 'package:flutter/material.dart';
import 'package:flutter_app/customwidget/scroll_widget/sliver_header.dart';
import 'package:flutter_app/util/DataUtil.dart';

import 'CustomScrollViewWidget.dart';
import 'GridViewWidget.dart';
import 'GridViewWidget2.dart';
import 'InfiniteListWidget.dart';
import 'ListViewWidget.dart';
import 'ScrollControllerWidget.dart';
import 'ScrollNotificationWidget.dart';
import 'SingleScrollView.dart';

/**
 * 滚动类组件
 */
class ScrollListWidget extends StatelessWidget {
  ScrollListWidget({
    Key ?key,
    @required this.text, // 接收一个text参数
  }) : super(key: key);
  final String ?text;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text(
          text??"",
        ),
      ),
      body: _ScrollListPage(),
    );
  }
}

class _ScrollListPage extends StatefulWidget {
  @override
  _ScrollPageState createState() => _ScrollPageState();
}

class _ScrollPageState extends State<_ScrollListPage> {
  var list = DataUtil.getScrollTitle();

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: list.length,
      itemBuilder: _buildItem,
      itemExtent: 60,
    );
  }
}

Widget _buildItem(BuildContext context, int index) {
  var list = DataUtil.getScrollTitle();
  return Container(
    height: double.infinity,
    color: Colors.white,
    child: GestureDetector(
      child: Container(
        margin: EdgeInsets.all(10),
        padding: EdgeInsets.all(10),
        color: Colors.cyan,
        child: Text(
          list[index],
          style: TextStyle(
            color: Colors.white,
          ),
        ),
      ),
      onTap: () {
        toArount(index, context, list[index]);
      },
    ),
  );
}

void toArount(int index, BuildContext context, String title) {
  switch (index) {
    case 0:
      goTo(context, SingleScrollView(text: title));
      break;
    case 1:
      goTo(context, ListViewWidget(text: title));
      break;
    case 2:
      goTo(context, InfiniteListWidget(text: title));
      break;
    case 3:
      goTo(context, GridViewWidget(text: title));
      break;
    case 4:
      goTo(context, GridViewWidget2(text: title));
      break;
    case 5:
      goTo(context, CustomScrollViewWidget(text: title));
      break;
    case 6:
      goTo(context, ScrollControllerWidget(text: title));
      break;
    case 7:
      goTo(context, ScrollNotificationWidget(text: title));
      break;
    case 8:
      goTo(context, SliverPersistentHeaderDemo(text: title));
      break;
  }
}

void goTo(BuildContext context, Widget widget) {
  Navigator.push(context, MaterialPageRoute(builder: (context) {
    return widget;
  }));
}
